package cn.huiyunche.admin.service.impl;

import cn.huiyunche.admin.service.DVcstyleOilService;
import cn.huiyunche.base.service.enums.OilProductsEnum;
import cn.huiyunche.base.service.mappers.DVcstyleOilMapper;
import cn.huiyunche.base.service.model.DVcstyleOil;
import cn.huiyunche.base.service.model.DVcstyleOilExample;
import cn.huiyunche.base.service.vo.DVcstyleOilVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by jxy on 17/1/5.
 */
@Service
public class DVcstyleOilServiceImpl implements DVcstyleOilService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DVcstyleOilServiceImpl.class);

    @Autowired
    private DVcstyleOilMapper dVcstyleOilMapper;

    public Map<String, Object> getListByPage(PageVo page,DVcstyleOilVo dVcstyleOilVo){
        LOGGER.info("getListByPage params page: {}",page);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> paramsMap = new HashMap<>();
        String orderByClause = StringUtils.isNotBlank(page.getOrder()) == true ? page.getOrder() : " id DESC";
        page.setTotalRecord(dVcstyleOilMapper.countByCondations(paramsMap));
        DVcstyleOilExample dVcstyleOilExample = new DVcstyleOilExample();
        dVcstyleOilExample.setLimitStart(page.getStartIndex());
        dVcstyleOilExample.setLimitEnd( page.getPageSize());
        dVcstyleOilExample.setOrderByClause(orderByClause);
        if (dVcstyleOilVo!= null){
            if (StringUtils.isNotBlank(dVcstyleOilVo.getName())){
                dVcstyleOilExample.createCriteria().andNameLike(dVcstyleOilVo.getName());
            }
            if (dVcstyleOilVo.getOilProducts() != null){
                dVcstyleOilExample.createCriteria().andOilProductsEqualTo(dVcstyleOilVo.getOilProducts());
            }
        }
        List<DVcstyleOil> dVcstyleOilList = dVcstyleOilMapper.selectByExample(dVcstyleOilExample);
        List<JSONObject> res = new ArrayList<>();
        for (DVcstyleOil dVcstyleOil : dVcstyleOilList){
            JSONObject json = (JSONObject)JSON.toJSON(dVcstyleOil);
            json.put("oilProductsText", OilProductsEnum.getByValue(json.getIntValue("oilProducts")) == null?"":OilProductsEnum.getByValue(json.getIntValue("oilProducts")).getText());
            res.add(json);
        }
        map.put("page", page);
        map.put("dVcstyleOilList", res);
        return map;
    }

    public void addDVcstyleOil(DVcstyleOil dVcstyleOil){
        LOGGER.info("addDOilPrice params dVcstyleOil: {}",dVcstyleOil);
        //校验车型编码是否存在
        if (!valideCodeExist(dVcstyleOil.getCode())){
            throw new BusinessException("车型编码已经存在");
        }
        //todo 校验油耗
        dVcstyleOil.setCreateTime(new Date());
        dVcstyleOil.setUpdateTime(new Date());
        int res = dVcstyleOilMapper.insertSelective(dVcstyleOil);
        if (res != 1){
            throw new BusinessException("数据插入车型油耗表失败");
        }

    }

    public void modifyDVcstyleOil(DVcstyleOil dVcstyleOil){
        LOGGER.info("modifyDVcstyleOil params dVcstyleOil: {}",dVcstyleOil);
        DVcstyleOil oldOil = dVcstyleOilMapper.selectByPrimaryKey(dVcstyleOil.getId());
        //当修改后的车型编码与原来车型编码不一致时  校验车型编码是否存在
        if (!dVcstyleOil.getCode().equals(oldOil.getCode())){
            if (!valideCodeExist(dVcstyleOil.getCode())){
                throw new BusinessException("车型编码已经存在");
            }
        }
        //todo 校验油耗
        dVcstyleOil.setUpdateTime(new Date());
        int res = dVcstyleOilMapper.updateByPrimaryKeySelective(dVcstyleOil);
        if (res != 1){
            throw new BusinessException("修改车型油耗数据失败");
        }

    }

    public boolean valideCodeExist(String code){
        DVcstyleOilExample dVcstyleOilExample = new DVcstyleOilExample();
       dVcstyleOilExample.createCriteria().andCodeEqualTo(code);
        List<DVcstyleOil> dVcstyleOilList = dVcstyleOilMapper.selectByExample(dVcstyleOilExample);
        if (dVcstyleOilList.size() > 0){
            return false;
        }
        return true;
    }

}
